﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jbe.Composite.Extensions;
using Microsoft.Practices.Composite.Regions;
using Jbe.TestSuite.Infrastructure.Interface;
using Jbe.TestSuite.Project.Properties;
using Jbe.TestSuite.Infrastructure.Interface.Documents;
using Jbe.TestSuite.Project.Presentation;
using Jbe.TestSuite.Infrastructure.Module.Applications;
using System.Windows.Input;

namespace Jbe.TestSuite.Project.Applications
{
    internal class ModuleController
    {
        private readonly IApplicationContainer container;
        private readonly IRegionManager regionManager;
        private readonly IUIElementCreationService uiElementCreationService;
        private readonly IDocumentManager documentManager;
        private ProjectViewPresentationModel presentationModel;


        public ModuleController(IApplicationContainer container, IRegionManager regionManager, IUIElementCreationService uiElementCreationService, IDocumentManager documentManager)
        {
            this.container = container;
            this.regionManager = regionManager;
            this.uiElementCreationService = uiElementCreationService;
            this.documentManager = documentManager;
        }


        public void Initialize()
        {
            //IMenuItem menuItem = uiElementCreationService.CreateMenuItem(Resources.ProjectMenu, Resources.EventLog.ToBitmap());
            //menuItem.Click += MenuItemClick;
            //regionManager.Regions[ShellRegionNames.ViewMenuItems].Add(menuItem);
            ShowProject();
        }

        private void MenuItemClick(object sender, EventArgs e)
        {
            ShowProject();
        }

        private void ShowProject()
        {
            if (presentationModel == null)
            {
                presentationModel = container.Resolve<ProjectViewPresentationModel>();
                presentationModel.View.DataContext = presentationModel;
                ((ProjectView)presentationModel.View).MouseDoubleClick += DoubleClickHandler;
                regionManager.Regions[ShellRegionNames.RightRegion].Add(presentationModel.View);
            }
            regionManager.Regions[ShellRegionNames.RightRegion].Activate(presentationModel.View);
        }

        void DoubleClickHandler(object sender, MouseEventArgs e)
        {
            if (ProjectItemPresentationModel.SelectedItem != null)
            {
                string file = ProjectItemPresentationModel.SelectedItem.File;
                ((DocumentManager)documentManager).OpenFile(file);
            }
        }
    }
}
